#include "Classes.h"

Player::Player(s32 CoordinateX, s32 CoordinateY, eCloakColour CloakColour_, eShieldType ShieldType_, eSwordType SwordType_) {
	// setup default values
	Action = ActionWalk;
	AnimationInterval = 150;
	Direction = South;

	// setup values from XML
	BoomerangType = BoomerangWooden;
	Colour = Colour::White;
	CloakColour = CloakColour_;
	ShieldType = ShieldType_;
	SwordType = SwordType_;
	X = CoordinateX;
	Y = CoordinateY;

	// setup initial images
	SetImages();
	SetImageRects();
	ImageRect = ImageRect1;
}
void Player::Act() {
	if(Acting) {
		for(u32 i = 0; i < Items.size(); i ++) {
			if(!Items[i]->getIsActive()) {
				Item* temp = Items[i];
				for(u32 i2 = 0; i2 < Zelda->Objects.size(); i2 ++) {
					if(Zelda->Objects[i2] == temp)
						Zelda->Objects.erase(Zelda->Objects.begin() + i2);
				}
				Items.erase(Items.begin()+i);
				delete temp;
			}
		}

		// player movement
		if(Zelda->Receiver->IsJoystickActive() == false) {
			if(Zelda->Receiver->IsKeyDown(KEY_LEFT) && Action != ActionUseItem) {
				Action = ActionWalk;
				if(Direction != West) {
					Direction = West;
					SetImageRects();
				}
				Animate();
				X -= Zelda->Scale;
			} else if(Zelda->Receiver->IsKeyDown(KEY_RIGHT) && Action != ActionUseItem) {
				Action = ActionWalk;
				if(Direction != East) {
					Direction = East;
					SetImageRects();
				}
				Animate();
				X += Zelda->Scale;
			} else if(Zelda->Receiver->IsKeyDown(KEY_UP) && Action != ActionUseItem) {
				Action = ActionWalk;
				if(Direction != North) {
					Direction = North;
					SetImageRects();
				}
				Animate();
				Y -= Zelda->Scale;
			} else if(Zelda->Receiver->IsKeyDown(KEY_DOWN) && Action != ActionUseItem) {
				Action = ActionWalk;
				if(Direction != South) {
					Direction = South;
					SetImageRects();
				}
				Animate();
				Y += Zelda->Scale;
			}

			// use item
			if(Zelda->Receiver->IsKeyClicked(KEY_KEY_B)) {
				Item* newItem = new Boomerang(this);
				UseItem(true);
			}
			if(Zelda->Receiver->IsKeyClicked(KEY_KEY_S)) {
				Item* newItem = new Sword(this);
				UseItem(true);
			}

			ResetAction();
		}
	}
}
void Player::Draw() {
	Zelda->Driver->draw2DImage(Zelda->SpritesLink,rect<s32>(X,Y,X + GetImageRect()->getWidth() * Zelda->Scale,Y + GetImageRect()->getHeight() * Zelda->Scale),*GetImageRect(),0,0,true);
}
eSwordType Player::GetSwordType() {
	return SwordType;
}
position2d<s32> Player::GetUseItemPosition(Item* CurrentItem) {
	switch (Direction) {
	case East:
		return position2d<s32>(X+15*Zelda->Scale,Y+8*Zelda->Scale);
		break;
	case South:
		return position2d<s32>(X+9*Zelda->Scale,Y+16*Zelda->Scale);
		break;
	case West:
		return position2d<s32>(X+1*Zelda->Scale,Y+8*Zelda->Scale);
		break;
	default:
		return position2d<s32>(X+5*Zelda->Scale,Y+1*Zelda->Scale);
		break;
	}
}
void Player::SetImages() {
	switch (CloakColour) {
	case GreenCloak:
		switch (ShieldType) {
		case SmallShield:
			ImageWalkEast1 = &Zelda->LinkImages[GreenSmallWalkEast1];
			ImageWalkEast2 = &Zelda->LinkImages[GreenSmallWalkEast2];
			ImageWalkSouth1 = &Zelda->LinkImages[GreenSmallWalkSouth1];
			ImageWalkSouth2 = &Zelda->LinkImages[GreenSmallWalkSouth2];
			ImageWalkWest1 = &Zelda->LinkImages[GreenSmallWalkWest1];
			ImageWalkWest2 = &Zelda->LinkImages[GreenSmallWalkWest2];
			break;
		}
		ImageWalkNorth1 = &Zelda->LinkImages[GreenWalkNorth1];
		ImageWalkNorth2 = &Zelda->LinkImages[GreenWalkNorth2];
		ImageLungeEast = &Zelda->LinkImages[GreenLungeEast];
		ImageLungeSouth = &Zelda->LinkImages[GreenLungeSouth];
		ImageLungeWest = &Zelda->LinkImages[GreenLungeWest];
		ImageLungeNorth = &Zelda->LinkImages[GreenLungeNorth];
		break;
	}
};
void Player::SetImageRects() {
	switch(Action) {
	case ActionWalk:
		switch (Direction) {
		case East:
			ImageRect1 = ImageWalkEast1;
			ImageRect2 = ImageWalkEast2;
			break;
		case South:
			ImageRect1 = ImageWalkSouth1;
			ImageRect2 = ImageWalkSouth2;
			break;
		case West:
			ImageRect1 = ImageWalkWest1;
			ImageRect2 = ImageWalkWest2;
			break;
		case North:
			ImageRect1 = ImageWalkNorth1;
			ImageRect2 = ImageWalkNorth2;
			break;
		}
		break;
	case ActionUseItem:
		switch (Direction) {
		case East:
			ImageRect = ImageLungeEast;
			break;
		case South:
			ImageRect = ImageLungeSouth;
			break;
		case West:
			ImageRect = ImageLungeWest;
			break;
		case North:
			ImageRect = ImageLungeNorth;
			break;
		}
		break;
	case ActionReset:
		ImageRect = ImageRect1;
		break;
	}
};